package com.example.demo.listener;

import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * ContextRefreshedEvent事件：bean初始化完成后执行
 * ContextClosedEvent 事件:容器销毁时执行
 **/
@Component
public class MyListener {

    @EventListener(value = ContextRefreshedEvent.class)
    public void invoke(ContextRefreshedEvent event){
        System.out.println("hahahaha~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("hahahahahaha event="+event.getSource());
    }

    @EventListener(value = ContextClosedEvent.class)
    public void closeEvent(ContextClosedEvent event){
        System.out.println("ContextClosedEvent~~~~~~~~~~~容器关闭");
        System.out.println("ContextClosedEvent~~~~~~~~~~~"+event.getSource());
    }

    @EventListener(value = ContextStoppedEvent.class)
    public void stopEvent(ContextStoppedEvent event){
        System.out.println("ContextStoppedEvent~~~~~~~~~~~容器关闭");
        System.out.println("ContextStoppedEvent~~~~~~~~~~~"+event.getSource());
    }
}
